-- extend UserData
-- 称号相关

local Title = import(".Title")
local UserData = import(".UserData")

-- 初始化称号相关数据
function UserData:initTitleData()
    self._activedID = 0 -- 激活的称号Id
    self._gotTitleIds = {} -- 已经获得的称号Id列表

    self._newGotTitleIds = {} -- 缓存新获得的称号Id列表

    self:_initTitles()
    -- self:reqTitleData()
    app.session:on("pb_title.S2C_DeleteTitle",function(ret)
        self:onDeleteTitle(ret)
    end)
end

-- 初始化徽章数据
function UserData:_initTitles()
    self._titleDict = {}
    local confs = GD:queryBadges()
    for k,v in pairs(confs) do
        local badgeTemp = Title.new(v)
        table.insert( self._titleDict, badgeTemp )
    end
    self.needsort = true
end
-- 返回全部 称号
function UserData:getTitles()
    if nil == self._titleDict then
        self:_initTitles()        
    end

    if UD:isSpTitleChange() then
        UD:resetSpTitleChange()
        self.needsort = true
    end

    if self.needsort then
        self:sortTitles()
        self.needsort = false
    end

    return self._titleDict
end

function UserData:sortTitles()
    table.sort( self._titleDict, function( a, b )
		local asort = tonumber(a:getSort()) or 1000000
		local bsort = tonumber(b:getSort()) or 1000000

		-- 排序规则：已激活(佩戴在身上)、新获得、已获得未激活、未获得(sort字段)
		-- if UD:isTitleActive( a:getId() ) ~= UD:isTitleActive( b:getId() ) then -- a激活
        --     return UD:isTitleActive( a:getId() )
        -- else
        if UD:isNewGotTitle( a:getId() ) ~= UD:isNewGotTitle( b:getId() ) then
            return UD:isNewGotTitle( a:getId() )
        elseif UD:isGotTitle( a:getId() ) ~= UD:isGotTitle( b:getId() ) then
            return UD:isGotTitle( a:getId() )
        else
            return asort < bsort
		end
	end)
end

-- 返回 titleId 的称号
function UserData:getTitleById( titleId )
    if nil == self._titleDict then
        self:_initTitles()
    end
    for i,v in ipairs( self._titleDict ) do
        if v:getId() == tonumber( titleId ) then
            return v
        end
    end
    return nil
end

function UserData:getTitleName(id)
    local titleName = ""
    local quality = 1
    if id and tonumber(id) > 0 then
        local title = self:getTitleById(id)
        if title then
            titleName = L(title:getName())
            quality = title:getQuality()
        end
    end
    return titleName, quality
end

-- 返回激活的titleId
function UserData:getActivedTitleId()
    return self._activedID
end

-- 判断 titleId 是否激活
function UserData:isTitleActive( titleId )
    return self._activedID == tonumber( titleId )
end

-- 判断一个称号ID是否获得
function UserData:isGotTitle( titleId )
    local isGot = false
    if table.indexof( self._gotTitleIds, titleId ) then
        isGot = true
    end

    if isGot == false then
        local spt = UD:getSpTitles()
        if spt and spt[titleId] then
            isGot = true
        end
    end

    return isGot
end

-- 判断一个称号ID是否新获得
function UserData:isNewGotTitle( titleId )
    local isNewGot = false
    if table.indexof( self._newGotTitleIds, titleId ) then
        isNewGot = true
    end

    if isNewGot == false then
        if UD:getNewSpTitles()[titleId] then
            isNewGot = true
        end
    end
    return isNewGot
end

-- 是否有新获得称号
function UserData:hasNewGotTitle()
    if 0 < #self._newGotTitleIds then
        return true
    end

    if table.hasvalue(UD:getNewSpTitles(), true) then
        return true
    end

    return false
end

-- 清空新获得称号列表
function UserData:clearNewGotTitleList()
    self._newGotTitleIds = {}
    UD._newSpTitles = {}
end

function UserData:clearNewGotTitle(aID)
    if self._newGotTitleIds and #self._newGotTitleIds > 0 then
        table.removebyvalue(self._newGotTitleIds, aID)
    end
    if UD._newSpTitles[aID] then
        UD._newSpTitles[aID] = nil
    end
end

------------------------------------------------>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- 拉取整个称号数据？
function UserData:reqTitleData()
    -- print( " -------------->>>>> UserData:reqTitleData " )
    app.session:request("pb_title.C2S_Titles", {}, function(ret)
        -- dump( ret, " -------------->>>>> 称号列表" )
        self._activedID = ret.activeID or 0
        self._gotTitleIds = {}
        for k,v in pairs( ret.titleIDs ) do
            table.insert( self._gotTitleIds, v )
        end
    end)
end

-- 激活 titleId 的称号
function UserData:reqActiveTitle( titleId , dontTips)
    -- print( " -------------->>>>> UserData:reqActiveTitle " .. titleId )
    app.session:request("pb_title.C2S_ActivateTitle", { ID = titleId }, function(ret)
        -- dump( ret, " -------------->>>>> " )
        if ret.success then -- 成功？
            self._activedID = titleId
            self.needsort = true
            if dontTips == nil then
            display.pushToast( L("lua_code_text_372") )
            end
        else
            display.pushToast( L("lua_code_text_224") )
        end
        app:sendMsg("NewBagController", "_refreshPanel", true)
    end)
end

-- 获得新称号
function UserData:onGotNewTitle( msg )
    -- print( " -------------->>>>> UserData:onGotNewTitle " .. msg.newID )
    if not table.indexof( self._newGotTitleIds, msg.newID ) then
        table.insert( self._newGotTitleIds, msg.newID )
    end
    if not table.indexof( self._gotTitleIds, msg.newID ) then
        table.insert( self._gotTitleIds, msg.newID )
    end
    self.needsort = true
    UD.redPoint:checkStateByKey(Constant.FUNC_TYPE_PACK_TITLE)
end

function UserData:onDeleteTitle(msg)
    if table.indexof( self._gotTitleIds, msg.titleID ) then
        table.removebyvalue(self._gotTitleIds, msg.titleID)
        app:sendMsg("NewBagController", "_refreshPanel", true)
    end
end


return UserData
